<?php
/**
 * 《便笺管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Code.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Note extends InfoUtil {
	public $tbl = Constant::tbl_note;

	public $status = array(
		'-' => '请选择',
		'1' => '待办事项',
		'2' => '完成事项',

		'9' => '其它',
	);
	
	public $orderby = array(
		'-' => '请选择',
		'emp' => '员工',
		'text' => '信息',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function layout($controller, $request) {
		$this->pretreat($controller);
		
		$emp = $_SESSION['id'];
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = 1 AND emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp);
		$result = $db->query($sql);
		
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			// $row['id'] = $row['note_id'];
			// $row['id'] = $row['id'];
			$row['text'] = $row['text'];
			
			$row['pos_x'] = intval($row['pos_x']);
			$row['pos_y'] = intval($row['pos_y']);
			$row['width'] = intval($row['width']);
			$row['height'] = intval($row['height']);
			
			$ret[] = $row;
		}
		
		$controller->assign_by_ref('result', json_encode($ret));

		$controller->display($request, 'info/note/layout');
		return true;
	}
	
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$controller->display($request, 'info/note/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$text_s = $request['text_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($name_s)) {
			$where .= " AND text LIKE '%" . $text_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
		
		if ($this->_audit('149') && $this->_audit('150')) {
			return '';
		}

		if ($this->_audit('149')) {
			return " office = '1'";
		}
		
		if ($this->_audit('150')) {
			return " office = '2'";
		}

		$emp = $_SESSION['id'];

		return sprintf("(emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/note/list');
		return true;
	}
	
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/note/edit');
		return true;
	}

	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		
		$text = $request['text'];
		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET office = '%s', emp = '%s', text = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $emp, $text, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function note_id($controller, $request) {		
		$emp = $_SESSION['id'];
		$office = $this->_office($emp);
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $emp, $emp);
		debug($sql);
		$db->query($sql);
		
		$ret['id'] = $db->insert_id();
		
		// echo json_encode($ret);
		echo $ret['id'];
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function insert($controller, $request) {
		$id = $request['id'];
		$text = $request['text'];
		
		$pos_x = $request['pos_x'];
		$pox_y = $request['pos_y'];
		$width = $request['width'];
		$height = $request['height'];
		
		$emp = $_SESSION['id'];
		$office = $this->_office($emp);
		
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, emp, note_id, text, pos_x, pos_y, width, height, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $emp, $id, $text, $pos_x, $pos_y, $width, $height, $emp);
		debug($sql);
		$db->query($sql);
		
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function update($controller, $request) {
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$id = $request['id'];
		
		$text = $request['text'];
		$pos_x = $request['pos_x'];
		$pox_y = $request['pos_y'];
		$width = $request['width'];
		$height = $request['height'];

		$emp = $_SESSION['id'];
		
		$db = newdb($this->dsn_w);
		// $fmt = "UPDATE %s SET text = '%s', pos_x = '%s', pos_y = '%s', width = '%s', height = '%s', modifytime = now() WHERE emp = '%s' AND note_id = '%s'";
		$fmt = "UPDATE %s SET text = '%s', pos_x = '%s', pos_y = '%s', width = '%s', height = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $text, $pos_x, $pox_y, $width, $height, $id);
		debug($sql);
		$db->query($sql);
		
		return true;
	}
	
	function hide($controller, $request) {
		$id = $request['id'];
		
		$emp = $_SESSION['id'];
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET status = 2, modifytime = now() WHERE emp = '%s' AND id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp, $id);
		debug($sql);
		$db->query($sql);
		
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/note/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function tj($controller, $request) {		
		$aa = $this->_list_a();
		// print_r($aa);
		if (!$aa || count($aa) == 0) {
			echo "33";
			return false;
		}
		
		$from = $this->email;
		$subject = '便笺信息';
				
		$d2 = $this->_tj_df("createtime", "%Y-%m-%d");
		$d3 = $this->_tj_df("modifytime", "%Y-%m-%d");
		
		$dd = $this->_tj_df_not("%Y-%m-%d");
		
		while (list($k, $row) = each($aa)) {
			$emp = $row['emp'];
						
			$e = Common::emp($emp);
			$to = $e['email'];
			if (empty($to) || $e['status'] == '3') {
				continue;
			}
			
			// $to = "sunxy@tecmold.com";		
			$cc = array(
				'待办事项'=>array('w' => "LENGTH(text) > 5 AND status = 1 AND emp = '$emp'", 'b'=>'id DESC'),
				'新增信息'=>array('w' => "LENGTH(text) > 5 AND $d2 AND emp = '$emp'", 'b'=>'id DESC'),
				'更新信息'=>array('w' => "LENGTH(text) > 5 AND $d3 AND $dd AND emp = '$emp'", 'b'=>'id DESC'),			
			);
			// print_r($cc);
			
			if ($this->_tj_cc($controller, $cc)) {
				echo ".";
				continue;
			}
	
			$controller->clear_assign('result');
			
			$body = $this->_tj_html($controller, $request, 'info/note/my/tj_list');
			
			echo $to . "\n";
			$this->mail_html($from, $to, $subject, $body);
		}

		logdate($controller, $request);
		return true;
	}
	
	function _list_a() {		
		$db = newdb($this->dsn_r);
		// $fmt = "SELECT emp, COUNT(*) t FROM %s WHERE LENGTH(text) > 5 GROUP BY emp";
		$fmt = "SELECT emp, COUNT(*) t FROM %s WHERE LENGTH(text) > 5 AND status = '1' GROUP BY emp";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		
		return $db->fetch_all($result);
	}
	
	function move($from, $to) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET emp = '%s' WHERE emp = '%s' AND status = 1";
		$sql = sprintf($fmt, $this->tbl, $to, $from);
		debug($sql);
		$db->query($sql);
	}
	
	function view_emp($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$ret = $this->_list_emp($id, '1');
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'info/note/_list');
		return true;
	}
}
?>